package com.zy.mc_module.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.viewpager.widget.ViewPager;

import com.zy.mc_module.R;

/**
 * Created on 18/10/20
 * 禁止ViewPager左右滑动
 */
public class NSViewPager extends ViewPager {

    //是否能够左右滑动
    private boolean isCanScroll = false;
    //是否设置切换动画
    private boolean smoothScroll = false;

    public NSViewPager(Context context) {
        super(context);
    }

    public NSViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttr(context, attrs);
    }

    private void initAttr(Context context, AttributeSet attrs) {
        if (null != attrs) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NSViewPager);
            isCanScroll = typedArray.getBoolean(R.styleable.NSViewPager_isCanScroll, false);
            smoothScroll = typedArray.getBoolean(R.styleable.NSViewPager_smoothScroll, false);
            typedArray.recycle();
        }
    }

    /**
     * 设置其是否能滑动换页
     *
     * @param isCanScroll false 不能换页， true 可以滑动换页
     */
    public void setScanScroll(boolean isCanScroll) {
        this.isCanScroll = isCanScroll;
    }

    public void setCurrentItem(int item) {
        super.setCurrentItem(item, smoothScroll);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return isCanScroll && super.onInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return isCanScroll && super.onTouchEvent(ev);
    }
}
